package com.tbit.uqbike.push.service.impl;

import com.tbit.uqbike.push.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service("redisService")
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void add(String key, String value) {
        ValueOperations<String, String> valueOperation = redisTemplate.opsForValue();
        valueOperation.set(key, value);
    }

    @Override
    public String get(String key) {
        ValueOperations<String, String> valueOperation = redisTemplate.opsForValue();
        return valueOperation.get(key);
    }

    @Override
    public String get(String hashId, String key) {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        return hashOperations.get(hashId, key);
    }

    @Override
    public Map<String, String> getAll(String hashId) {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        return hashOperations.entries(hashId);
    }

    @Override
    public void set(String hashId, String key, String value) {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        hashOperations.put(hashId, key, value);
    }


    @Override
    public void delet(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public void add(String key, String value, Integer seconds) {
        ValueOperations<String, String> valueOperation = redisTemplate.opsForValue();
        valueOperation.set(key, value, seconds, TimeUnit.SECONDS);

    }

    @Override
    public List<String> getList(String key) {

        List<String> values = null;
        ListOperations<String, String> listOpreations = redisTemplate.opsForList();
        values = listOpreations.range(key, 0, -1);
        return values;
    }

    @Override
    public void andToList(String key, String value) {
        ListOperations<String, String> listOpreations = redisTemplate.opsForList();
        listOpreations.rightPush(key, value);
    }
}  